<?php
/**
 * Class that operate on table 'hs_user'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsUserMySqlDAO implements HsUserDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsUserMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_user WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_user';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_user ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsUser primary key
 	 */
	public function delete($userID){
		$sql = 'DELETE FROM hs_user WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($userID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsUserMySql hsUser
 	 */
	public function insert($hsUser){
		$sql = 'INSERT INTO hs_user (firstName, lastName, userName, nickName, email, sex, birthDay, zipCode, visitCount, userLevel, relationshipStatus, currentStatus, statusTimestamp, statusCount, messageCount, albumCount, selfDisclosure, userPwd, salt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsUser->firstName);
		$sqlQuery->set($hsUser->lastName);
		$sqlQuery->set($hsUser->userName);
		$sqlQuery->set($hsUser->nickName);
		$sqlQuery->set($hsUser->email);
		$sqlQuery->setNumber($hsUser->sex);
		$sqlQuery->set($hsUser->birthDay);
		$sqlQuery->set($hsUser->zipCode);
		$sqlQuery->setNumber($hsUser->visitCount);
		$sqlQuery->setNumber($hsUser->userLevel);
		$sqlQuery->setNumber($hsUser->relationshipStatus);
		$sqlQuery->set($hsUser->currentStatus);
		$sqlQuery->set($hsUser->statusTimestamp);
		$sqlQuery->setNumber($hsUser->statusCount);
		$sqlQuery->setNumber($hsUser->messageCount);
		$sqlQuery->setNumber($hsUser->albumCount);
		$sqlQuery->set($hsUser->selfDisclosure);
		$sqlQuery->set($hsUser->userPwd);
		$sqlQuery->set($hsUser->salt);

		$id = $this->executeInsert($sqlQuery);	
		$hsUser->userID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsUserMySql hsUser
 	 */
	public function update($hsUser){
		$sql = 'UPDATE hs_user SET firstName = ?, lastName = ?, userName = ?, nickName = ?, email = ?, sex = ?, birthDay = ?, zipCode = ?, visitCount = ?, userLevel = ?, relationshipStatus = ?, currentStatus = ?, statusTimestamp = ?, statusCount = ?, messageCount = ?, albumCount = ?, selfDisclosure = ?, userPwd = ?, salt = ? WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsUser->firstName);
		$sqlQuery->set($hsUser->lastName);
		$sqlQuery->set($hsUser->userName);
		$sqlQuery->set($hsUser->nickName);
		$sqlQuery->set($hsUser->email);
		$sqlQuery->setNumber($hsUser->sex);
		$sqlQuery->set($hsUser->birthDay);
		$sqlQuery->set($hsUser->zipCode);
		$sqlQuery->setNumber($hsUser->visitCount);
		$sqlQuery->setNumber($hsUser->userLevel);
		$sqlQuery->setNumber($hsUser->relationshipStatus);
		$sqlQuery->set($hsUser->currentStatus);
		$sqlQuery->set($hsUser->statusTimestamp);
		$sqlQuery->setNumber($hsUser->statusCount);
		$sqlQuery->setNumber($hsUser->messageCount);
		$sqlQuery->setNumber($hsUser->albumCount);
		$sqlQuery->set($hsUser->selfDisclosure);
		$sqlQuery->set($hsUser->userPwd);
		$sqlQuery->set($hsUser->salt);

		$sqlQuery->set($hsUser->userID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_user';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFirstName($value){
		$sql = 'SELECT * FROM hs_user WHERE firstName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLastName($value){
		$sql = 'SELECT * FROM hs_user WHERE lastName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserName($value){
		$sql = 'SELECT * FROM hs_user WHERE userName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNickName($value){
		$sql = 'SELECT * FROM hs_user WHERE nickName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM hs_user WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySex($value){
		$sql = 'SELECT * FROM hs_user WHERE sex = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBirthDay($value){
		$sql = 'SELECT * FROM hs_user WHERE birthDay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByZipCode($value){
		$sql = 'SELECT * FROM hs_user WHERE zipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisitCount($value){
		$sql = 'SELECT * FROM hs_user WHERE visitCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserLevel($value){
		$sql = 'SELECT * FROM hs_user WHERE userLevel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByRelationshipStatus($value){
		$sql = 'SELECT * FROM hs_user WHERE relationshipStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCurrentStatus($value){
		$sql = 'SELECT * FROM hs_user WHERE currentStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatusTimestamp($value){
		$sql = 'SELECT * FROM hs_user WHERE statusTimestamp = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatusCount($value){
		$sql = 'SELECT * FROM hs_user WHERE statusCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMessageCount($value){
		$sql = 'SELECT * FROM hs_user WHERE messageCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAlbumCount($value){
		$sql = 'SELECT * FROM hs_user WHERE albumCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySelfDisclosure($value){
		$sql = 'SELECT * FROM hs_user WHERE selfDisclosure = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserPwd($value){
		$sql = 'SELECT * FROM hs_user WHERE userPwd = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySalt($value){
		$sql = 'SELECT * FROM hs_user WHERE salt = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFirstName($value){
		$sql = 'DELETE FROM hs_user WHERE firstName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLastName($value){
		$sql = 'DELETE FROM hs_user WHERE lastName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserName($value){
		$sql = 'DELETE FROM hs_user WHERE userName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNickName($value){
		$sql = 'DELETE FROM hs_user WHERE nickName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM hs_user WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySex($value){
		$sql = 'DELETE FROM hs_user WHERE sex = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBirthDay($value){
		$sql = 'DELETE FROM hs_user WHERE birthDay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByZipCode($value){
		$sql = 'DELETE FROM hs_user WHERE zipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisitCount($value){
		$sql = 'DELETE FROM hs_user WHERE visitCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserLevel($value){
		$sql = 'DELETE FROM hs_user WHERE userLevel = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByRelationshipStatus($value){
		$sql = 'DELETE FROM hs_user WHERE relationshipStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCurrentStatus($value){
		$sql = 'DELETE FROM hs_user WHERE currentStatus = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatusTimestamp($value){
		$sql = 'DELETE FROM hs_user WHERE statusTimestamp = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatusCount($value){
		$sql = 'DELETE FROM hs_user WHERE statusCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMessageCount($value){
		$sql = 'DELETE FROM hs_user WHERE messageCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAlbumCount($value){
		$sql = 'DELETE FROM hs_user WHERE albumCount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySelfDisclosure($value){
		$sql = 'DELETE FROM hs_user WHERE selfDisclosure = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserPwd($value){
		$sql = 'DELETE FROM hs_user WHERE userPwd = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySalt($value){
		$sql = 'DELETE FROM hs_user WHERE salt = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsUserMySql 
	 */
	protected function readRow($row){
		$hsUser = new HsUser();
		
		$hsUser->userID = $row['userID'];
		$hsUser->firstName = $row['firstName'];
		$hsUser->lastName = $row['lastName'];
		$hsUser->userName = $row['userName'];
		$hsUser->nickName = $row['nickName'];
		$hsUser->email = $row['email'];
		$hsUser->sex = $row['sex'];
		$hsUser->birthDay = $row['birthDay'];
		$hsUser->zipCode = $row['zipCode'];
		$hsUser->visitCount = $row['visitCount'];
		$hsUser->userLevel = $row['userLevel'];
		$hsUser->relationshipStatus = $row['relationshipStatus'];
		$hsUser->currentStatus = $row['currentStatus'];
		$hsUser->statusTimestamp = $row['statusTimestamp'];
		$hsUser->statusCount = $row['statusCount'];
		$hsUser->messageCount = $row['messageCount'];
		$hsUser->albumCount = $row['albumCount'];
		$hsUser->selfDisclosure = $row['selfDisclosure'];
		$hsUser->userPwd = $row['userPwd'];
		$hsUser->salt = $row['salt'];

		return $hsUser;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsUserMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>